<?php 
//  vim:set fdm=indent ts=2 ai et sts=2 sw=2 tw=0:
//  simple database query class by $Author: anton $ 
//  $Date: 2008-02-26 11:31:53 +0800 (二, 26  2月 2008) $
//  xml.php
//  XML Parser 定義 $URL
//  Released as a GPL licensed software.

function GetElementPropertyByName($packet, $tag, $name)
{
  $patern = "/<$tag.* $name=\"([^\"]*)\".*>?.*<\/$tag>/";
  $ret = preg_match($patern, $packet, $matches);
  $ans = $matches[1];
  return ($ret)?$ans:NULL;
}

function GetElementByName ($xml, $tag)
{
  $xml = str_replace("\n"," ",$xml);
  $ret = preg_match("/\<$tag.*?\>(.*?)<\/$tag\>/",$xml,$matches);
  return ($ret)?$matches[1]:NULL;
}

function GetElementByNameAll ($xml, $tag)
{
  $xml=str_replace("\n"," ",$xml);
  $ret = preg_match_all("/\<$tag.*?\>(.*?)<\/$tag\>/",$xml,$matches);
  return ($ret)?$matches[1]:NULL;
}

function XPathValue($XPath,$XML)
{
  $startpos = strpos($XPath, "/");
  if ($startpos)
    $XPathArray = explode("/",$XPath);
  else
    $XPathArray = array(0=>$XPath);

  $node = $XML;
  while (list($key,$value) = each($XPathArray)) {
    if ($value!="") 
    {
        $node = GetElementByName($node, $value);
        break;
    }
  }
  $node = str_replace("\n"," ",$node);
  return $node;
}

function GetSodaArr()
{
  $xml="";
  $keyword="entry/summary";
  $fs =fopen("data.xml","r");
  while (!feof($fs))
     $xml.=fread($fs,1024);
  $xml=str_replace("\n"," ",$xml);
  fclose($fs);

  $arra = GetElementByNameAll($xml,"entry");
  $summary = Array();
  $title   = Array();
  $daterec = Array();
  for ($i=0;$i<count($arra);$i++)
  {
    $temp = GetElementByNameAll($arra[$i],"title");
    $temp = str_replace("&nbsp;"," ",html_entity_decode($temp[0]));
    array_push($title,$temp);
    $temp = GetElementByNameAll($arra[$i],"summary");
    $temp = str_replace("&nbsp;"," ",html_entity_decode($temp[0]));
    array_push($summary,$temp);
    preg_match("/ (200[0-9]-.*[0-9])/",$temp,$matches);
    array_push($daterec,$matches[1]);
  }

  $arrb= Array(0=>$title, 1=>$daterec, 2=>$summary);
  return $arrb;
}

function GetSoda($date)
{
  echo "查詢字串：'$date'\n";
  $date = Human($date);
  echo "修改：'$date'\n";
  $fount = false;
  $arra = GetSodaArr();
  $ans = Array();
  for ($i=0;$i<count($arra[0]);$i++)
  {
    if (strstr($arra[1][$i],$date) || strstr($arra[0][$i],$date))
    {
      $found = true;
      $arrb = Array(0=>$arra[0][$i],$arra[1][$i],$arra[2][$i]);
      array_push($ans,$arrb);
    }
  }

  if ($found)
    return $ans;
  else
    return NULL;
}

function Human($str)
{
  $orig  = Array(
  "三十一","三十","二十九","二十八","二十七",
  "二十六","二十五","二十四","二十三","二十二",
  "二十一","二十","十九","十八","十七",
  "十六","十五","十四","十三","十二",
  "十一","十","一","二","三",
  "四","五","六","七","八","九");
  $subst = Array("31","30",
  "29","28","27","26","25","24","23","22","21","20",
  "19","18","17","16","15","14","13","12","11",
  "10","1","2","3","4","5","6","7","8","9"); 

  $str = str_replace($orig,$subst,$str);
  $now = time();
  switch ($str)
  {
    case "1月":
      $ans = "-01-";
      return $ans;
      break;
    case "2月":
      $ans = "-02-";
      return $ans;
      break;
    case "3月":
      $ans = "-03-";
      return $ans;
      break;
    case "4月":
      $ans = "-04-";
      return $ans;
      break;
    case "5月":
      $ans = "-05-";
      return $ans;
      break;
    case "6月":
      $ans = "-06-";
      return $ans;
      break;
    case "7月":
      $ans = "-07-";
      return $ans;
      break;
    case "8月":
      $ans = "-08-";
      return $ans;
      break;
    case "9月":
      $ans = "-09-";
      return $ans;
      break;
    case "10月":
      $ans = "-10-";
      return $ans;
      break;
    case "11月":
      $ans = "-11-";
      return $ans;
      break;
    case "12月":
      $ans = "-12-";
      return $ans;
      break;
    case NULL:
    case "昨天":
      $ans = $now - 86400;
      break;
    case "今天":
      $ans = $now;
      break;
    case "明天":
      $ans = $now + 86400;
      break;
    default:
      return $str;
      break;
  }
  $date = strftime("%Y-%m-%d",$ans);
  return $date;
}
?>
